import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home',
    component: () => import('@/components/base-main'),
    children: [
      {
        path: '/home',
        name: 'home',
        component: () => import('@/view/home'),
      },
      {
        path: '/service',
        name: 'service',
        component: () => import('@/view/service'),
      },
      {
        path: '/mine',
        name: 'mine',
        component: () => import('@/view/mine'),
      }
    ],
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/view/Login'),
  },
  {
    path: '/stepsbefore',
    name: 'stepsbefore',
    component: () => import('@/view/stepsbefore'),
  },
  {
    path: '/certificate',
    name: 'certificate',
    component: () => import('@/view/certificate'),
  },
  {
    path: '/signup',
    name: 'signup',
    component: () => import('@/view/signup'),
  },
  {
    path: '/temp',
    name: 'temp',
    component: () => import('@/view/temp'),
  },
  {
    path: '/temp1',
    name: 'temp1',
    component: () => import('@/view/temp1'),
  },
];

const router = new VueRouter({
  mode: 'hash',
  base: '/',
  routes,
});

router.beforeEach((to, from, next) => {
  if (!sessionStorage.getItem('userid')
      && to.name !== 'Login'
      && to.name !== 'signup'
  ) {
    next({ name: 'Login' });
  } else {
    next();
  }
});

export default router;
